package com.liu.FileReader_;

import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReader00 {
    public static void main(String[] args) {

    }
    @Test
    public void test1() {
        String filepath = "d:\\";
        String filename = "study_1.txt";
        File file = new File(filepath,filename);
        try {
            file.createNewFile() ;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void test2() {
        String filepath = "d:\\study_1.txt";
        int readLen = 0;
        FileReader fileReader = null;
        try {
             fileReader = new FileReader(filepath);
             while ((readLen = fileReader.read()) != -1) {
                 System.out.print((char) readLen);
             }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close() ;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Test
    public void test3() {
        String filepath = "d:\\study_1.txt";
        int readLen = 0;
        char[] chars = new char[8];
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(filepath);
            while ((readLen = fileReader.read(chars)) != -1) {
                System.out.print(new String(chars,0,readLen));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close() ;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
